//
//  KeyboardViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2019/12/12.
//  Copyright © 2019 onemeeting. All rights reserved.
//

import Foundation

class KeyboardViewController: BaseViewController {
    
    //let lastValidTimestampCount = ""
    
    var keyNumW = CGFloat(80)
    var keyNumH =  CGFloat(54)
    
    let hnum = 3
    let vnum = 4
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.layer?.backgroundColor = NSColor.black.cgColor
        setupUI()
    }
    
    
    lazy var backBtn: LLCustomBtn = {
        let btn = LLCustomBtn()
        btn.isHandCursor = true
        btn.defaultBackgroundImage = NSImage.init(named: NSImage.Name.init("icon_navigate_updown"))
        btn.action = #selector(backBtnAction)
        btn.target = self
        return btn
    }()
    
    lazy var boardView: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.clear.cgColor
        view.layer?.borderColor = color_main140.cgColor
        view.layer?.borderWidth = 1
        view.layer?.masksToBounds = true
        view.layer?.cornerRadius = 5
        return view
    }()
    
    lazy var textField: BaseTextField = {
        let view = BaseTextField()
        view.placeholderString = "请输入会议号"
        view.textColor = NSColor.white
        view.cell?.font = NSFont.systemFont(ofSize: 18)
        view.formatter = IntegerValueFormatter()
        view.cell?.lineBreakMode = .byTruncatingTail
        view.delegate = self
        return view
    }()
    
    lazy var clearBtn: ZHButton = {
        let btn = ZHButton()
        btn.normalImage = NSImage.init(named: NSImage.Name.init("ic_clear"))!
        btn.hoverImage = NSImage.init(named: NSImage.Name.init("ic_clear_high"))!
        btn.action = #selector(clearBtnAction)
        btn.target = self
        return btn
    }()
  
    
    lazy var joinBtn: LLCustomBtn = {
        let btn = LLCustomBtn()
        btn.defaultTitle = "加入会议"
        btn.defaultBackgroundColor = color_main140
        btn.defaultTitleColor = color_gray(256)
        btn.defaultFont = NSFont.systemFont(ofSize: 14)
        btn.textAlignment = LLTextAlignmentCenter
        btn.action = #selector(joinBtnAction)
        btn.target = self
        return btn
    }()
    
    func setupUI()  {
        self.view.addSubview(backBtn)
        backBtn.snp.makeConstraints { (make) in
            make.height.width.equalTo(20)
            make.right.equalTo(-30)
            make.top.equalTo(20)
        }
        self.view.addSubview(boardView)
        boardView.snp.makeConstraints { (make) in
            make.width.equalTo(260)
            make.height.equalTo(42)
            make.centerX.equalToSuperview()
            make.top.equalTo(20)
        }
        boardView.addSubview(clearBtn)
        clearBtn.snp.makeConstraints { (make) in
            make.width.equalTo(20)
            make.height.equalTo(12)
            make.right.equalTo(-10)
            make.centerY.equalToSuperview()
        }
        
        boardView.addSubview(textField)
        textField.snp.makeConstraints { (make) in
            make.width.equalTo(150)
            make.height.equalTo(20)
            make.center.equalToSuperview()
        }
        textField.zh_border(radius: 5, borderColor: NSColor.white, borderWidth: 0)
        
        
        self.view.addSubview(joinBtn)
        joinBtn.snp.makeConstraints { (make) in
            make.width.equalTo(180)
            make.height.equalTo(32)
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-30)
        }
        joinBtn.zh_border(radius: 15, borderColor: nil, borderWidth: 0)
        
        self.view.addSubview(keybord)
        keybord.snp.makeConstraints { (make) in
            make.width.equalTo(keyNumW*CGFloat(hnum))
            make.height.equalTo(keyNumH*CGFloat(vnum))
            make.top.equalTo(70)
            make.centerX.equalToSuperview()
        }
        self.initKeyBorad()
    }
    
    lazy var keybord: NSView = {
        let view = NSView()
        return view
    }()
    
    func initKeyBorad() {
        
        for i in 0..<vnum {
            for j in 0..<hnum {
                let inx = i*hnum+j
                let btn = ZHButton()
                btn.title = "5"
                btn.zh_cornerRadius = 3.0
                btn.zh_color = NSColor.white
                btn.hoverColor = color_gray(40)
                btn.zh_backgroundColor = NSColor.black
                btn.backgroundHoverColor = color_gray(80)
                btn.font = NSFont.systemFont(ofSize: 18)
                keybord.addSubview(btn)
                btn.zh_border(radius: 5, borderColor: nil, borderWidth: 0)
                btn.snp.makeConstraints { (make) in
                     make.width.equalTo(keyNumW)
                     make.height.equalTo(keyNumH)
                     make.left.equalTo(CGFloat(j)*(keyNumW))
                     make.top.equalTo(CGFloat(i)*(keyNumH))
                }
                btn.target = self
                btn.action = #selector(keyboardAction(senber:))
                switch inx {
                case 9,11:
                    btn.isHidden = true
                    btn.title = ""
                default:
                    btn.isHidden = false
                    btn.title = String(inx+1)
                    if inx == 10 {
                        btn.title = "0"
                    }
                }
                
                
            }
        }
    }
    
    @objc func keyboardAction(senber:NSButton) {
        let num = senber.title
        if self.textField.stringValue.count < KEYBOARD_MAX_LENGTH {
            if num != "" {
                if self.textField.stringValue == "" {
                    self.textField.stringValue = num
                }else {
                    self.textField.stringValue = self.textField.stringValue+num
                }
            }
        }
        
    }
    
    @objc func clearBtnAction() {
        self.textField.stringValue = ""
    }
    
    @objc func backBtnAction()  {
        self.dismiss(nil)
    }
    
    @objc func joinBtnAction()  {
        self.dismiss(nil)
    }
    
    
}

extension KeyboardViewController: NSTextFieldDelegate{
    
    func controlTextDidChange(_ obj: Notification) {
        let textfixd = obj.object as? NSTextField
        /*
        let str = Regular.RegularExpression(regex: "^[d,]{1,6}$", validateString: textfixd!.stringValue)
        if str.count >= 0 {
            textfixd?.stringValue = lastValidTimestampCount
        }
        */
        if (textfixd?.stringValue.count)! > KEYBOARD_MAX_LENGTH{
            textfixd?.stringValue = (textfixd?.stringValue.substring(toIndex: KEYBOARD_MAX_LENGTH))!
        }
    }
    
}
